<?php
//
// +--------------------------------------+
// |PHP version 5.6
// +--------------------------------------+
// |知果汇后台登录模块
// +--------------------------------------+
// |2017-12-26
// +--------------------------------------+
// |潘龙兵
// +--------------------------------------+
// |知果汇
// +--------------------------------------+
//
namespace app\api\controller;

use think\View;
use think\Controller;
use think\Db;
use \app\data\Data;
use think\Request;

class Index extends Common
{
    public function __construct()
    {
        parent::__construct();
        // header("Access-Control-Allow-Credentials: true");
        header("Access-Control-Allow-Origin:*"); // 指定允许其他域名访问
        header("Access-Control-Allow-Methods:POST,GET"); // 响应类型
        header("Access-Control-Allow-Headers:x-requested-with,content-type,Accept");  // 响应头设置
        // header("Content-type:text/html;charset=utf-8");
    }


    /**
     * @知果汇后台
     * 按json方式输出通信数据
     * @2017-12-26
     * @author 潘龙兵
     * @param integer $code 状态码
     * @param string $message 提示信息
     * @param array $data 数据
     * return string
     */
    protected static function json($code, $message = '', $data)
    {
        if (!is_numeric($code)) {
            return '';
        }
        $result = array(
            'code' => $code,
            'message' => $message,
            'data' => $data
        );
        echo json_encode($result);
        exit;
    }

    /**
     * @知果汇后台
     * 接口输出状态码规范
     * @2017-12-26
     * @author 潘龙兵
     * @param integer $code 状态码
     * @param string $message 提示信息
     * return string
     */
    public static function code()
    {
        $data = [
            '200' => '请求成功，数据返回成功',
            '400' => '请求提交格式不正确',
            '1000' => '请求失败，数据返回失败',
        ];
        echo json_encode($data);
        exit;
    }


    /**
     * @知果汇后台
     * 按jsonp方式输出通信数据
     * @2018-01-23
     * @author 潘龙兵
     * @param integer $code 状态码
     * @param string $message 提示信息
     * @param array $data 数据
     * return string
     */
    protected static function _jsonp($code, $message = '', $data)
    {
        if (!is_numeric($code)) {
            return '';
        }
        $data = array(
            'code' => $code,
            'message' => $message,
            'data' => $data
        );
        $callback = $_GET['callback'];
        echo $callback . '(' . json_encode($data) . ')';
        exit;
    }

     public function index()
    {
        $limit = 4;

        $where5['status'] = 1;
        $res['banner'] = db('banner')->limit($limit)->where($where5)->select();

        $where3['is_show'] = 1;
        $where3['status'] = 1;
        $where3['static'] = 2;
        $res['category1'] = db('market_trading')->where('category_id', 15)->where($where3)->count();
        $res['category2'] = db('market_trading')->where('category_id', 16)->where($where3)->count();
        $res['category3'] = db('market_trading')->where('category_id', 17)->where($where3)->count();
//        $res['category4'] = db('market_trading')->where('p_id', 14)->where($where3)->count();

        $where2['status'] = 1;
        $where2['state'] = 1;
        $res['services1'] = db('service_market')->where('classify', 4)->where($where2)->count();
        $res['services2'] = db('service_market')->where('classify', 2)->where($where2)->count();
        $res['services3'] = db('service_market')->where('classify', 6)->where($where2)->count();

        $where1['status'] = 1;
        $res['video'] = db('video_list')->where($where1)->limit($limit)->select();

		
		
        $where5['a.status'] = 1;
        $join[0] = "__TALENT_TYPE__ t";
        $join[1] = "t.id = a.talentrole";
        $filed1 = "a.id,a.talentname,a.label,a.img_url,a.field,t.rolename";
        $res['talent_1_count'] = db('talent')->alias('a')->where('sheet', 1)->where($where5)->join($join[0], $join[1])->count();
        $res['talent_2_count'] = db('talent')->alias('a')->where('sheet', 2)->where($where5)->join($join[0], $join[1])->count();
        $res['talent_1'] = db('talent')->order("rand()")->alias('a')->where('sheet', 1)->where($where5)->join($join[0], $join[1])->field($filed1)->limit(3)->select();
        $res['talent_2'] = db('talent')->order("rand()")->alias('a')->where('sheet', 2)->where($where5)->join($join[0], $join[1])->field($filed1)->limit(3)->select();

		
        $where4['status'] = 1;
        $filed = 'id,pic,title,type,time';
        $res['government1'] = db('government_work')->field($filed)->where('type', 1)->limit(5)->where($where4)->select();
        $res['government2'] = db('government_work')->field($filed)->where('type', 2)->limit(5)->where($where4)->select();
        $res['government3'] = db('government_work')->field($filed)->where('type', 3)->limit(5)->where($where4)->select();
        $res['government4'] = db('government_work')->field($filed)->where('type', 4)->limit(5)->where($where4)->select();

        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '首页信息查询成功', $res);
        } else {
            $this->_jsonp('1000', '首页信息查询失败', null);
        }
    }


}

?>